package com.controlled;


import com.entity.User;
import com.mapper.UserMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.Map;

//http://localhost:8181/user/

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @GetMapping("/test")
    public String test(){
        return "yes";
    }

    @GetMapping("/findAll")
    public List<User> findAll(){
        List<User> users=userMapper.selectList(null);
        return users;
    }

    //shiro的测试
    @RequestMapping("/add")
    public String add(){
        return "add";
    }
    @RequestMapping("/update")
    public String update(){
        return "update";
    }//http://localhost:8181/user/update
    //shiro跳转提示页面
    @RequestMapping("/tologin")
    public String tologin(){
        return "你没登录，请登录";
    }
    @RequestMapping("/login")
    public  String login(String username, String password, Model model){
        //获取当前的用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户的登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            subject.login(token);
            return "/test";
        }catch (UnknownAccountException e){//用户不存在
            model.addAttribute("msg"," 用户不存在！！！");
            return "/login";
        }
        catch (IncorrectCredentialsException e){//密码错误
            model.addAttribute("msg"," 密码错误！！！");
            return "/login";
        }
    }
    @RequestMapping("/logintest/{id}/{name}")//http://localhost:8181/user/logintest/4/Sandy
    public String logintest(@PathVariable("id") String id,@PathVariable("name") String name){
        Model model=new Model() {
            @Override
            public Model addAttribute(String s, Object o) {
                return null;
            }

            @Override
            public Model addAttribute(Object o) {
                return null;
            }

            @Override
            public Model addAllAttributes(Collection<?> collection) {
                return null;
            }

            @Override
            public Model addAllAttributes(Map<String, ?> map) {
                return null;
            }

            @Override
            public Model mergeAttributes(Map<String, ?> map) {
                return null;
            }

            @Override
            public boolean containsAttribute(String s) {
                return false;
            }

            @Override
            public Object getAttribute(String s) {
                return null;
            }

            @Override
            public Map<String, Object> asMap() {
                return null;
            }
        };
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(id, name);
        try {
            subject.login(token);
            return "登录成功";
        }catch (UnknownAccountException e){//用户不存在
            model.addAttribute("msg"," 用户不存在！！！");
            return "/login";
        }
        catch (IncorrectCredentialsException e){//密码错误
            model.addAttribute("msg"," 密码错误！！！");
            return "/login";
        }
    }

    @RequestMapping("/noauth")
    @ResponseBody
    public String unauthorized(){
        return "未经授权不许进入此页面";
    }
}

